/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin;

import entity.Groups;

import entity.Users;
import facade.GroupsFacadeLocal;
import facade.RoleFacadeLocal;
import facade.UsersFacadeLocal;
import java.io.IOException;
import java.util.Collection;

import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.ConfigurationManager;

/**
 *
 * @author Tatyana
 */
@ServletSecurity(@HttpConstraint(rolesAllowed={"Administrator"}))
@WebServlet(name = "UserView", urlPatterns = {"/userView"})
public class UserView extends HttpServlet {

    @EJB
    private RoleFacadeLocal roleFacade;
    @EJB
    private GroupsFacadeLocal groupsFacade;
    @EJB
    private UsersFacadeLocal usersFacade;
    protected String sortBy = null;
    protected String keyword = null;
    private String adminpage =ConfigurationManager.getInstance().getLinksProperties().getProperty("ADMIN_PAGE");
            
    
 

    public void init(ServletConfig servletConfig) throws ServletException {
        this.sortBy = servletConfig.getInitParameter("SortBy");
    }

    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {

        int page = 1;
        int groupsPage =1;
        int recordsPerPage = 5;
        if (request.getParameter("page") != null) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (request.getParameter("groupPage") != null) {
            groupsPage = Integer.parseInt(request.getParameter("groupPage"));
        }
        

        List<Users> users = null;
       //        if (request.getParameter("SortBy") != null) {
//            sortBy = request.getParameter("SortBy");}

        if (request.getParameter("keyword") != null) {

            keyword = request.getParameter("keyword");
            users = usersFacade.findByKeyword((page - 1) * recordsPerPage, recordsPerPage, keyword);
        } else {

            if (request.getParameter("SortBy") != null) {

                sortBy = request.getParameter("SortBy");
                users = usersFacade.findRangeSortBy(page, sortBy);
            } else {
                users = usersFacade.findRangeSortBy(page, sortBy);
            }
        }
        int noOfRecords = usersFacade.count();
        int noOfGroupRecords = groupsFacade.count();
        
        int noOfGroupPages = (int) Math.ceil(noOfGroupRecords * 1.0 / recordsPerPage);
        int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);

        request.setAttribute("users", users);
        request.setAttribute("noOfPages", noOfPages);
        request.setAttribute("noOfGroupPages", noOfGroupPages); 
        request.setAttribute("currentPage", page);
        request.setAttribute("currentGroupPage", groupsPage);
        

        //Collection<Groups> groups = groupsFacade.findAll();
        Collection<Groups> groups=groupsFacade.loadRangeOfGroups(groupsPage,recordsPerPage);
        request.setAttribute("mygroups", groups);
        RequestDispatcher view = request.getRequestDispatcher(adminpage);
        view.forward(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
